import terminal from '@CRM/api/modules/terminal.js'
import common from '../../api/modules/common'

export const company = {
  namespaced: true,
  state: {
    headOptions: [],
    permission: [],
    department:[]
  },
  mutations: {
    setHandlHead(state, heads) {
      state.headOptions = heads
    },
    setMenuPermission(state, permission) {
      state.permission = permission
    },
    setDepartment(state, permission) {
      state.department = permission
    }
  },
  actions: {
    // 从后台获取全部
    async getHandlHead({ commit, state }, types) {
      var res = await terminal().HEAD_PEOPLE(1)
      if (res.code === 0) {
        state.headOptions = res.data || []
        commit('setHandlHead', res.data)
        return state.headOptions
      }
    },
    // 从后台获取全部
    async getMenuPermission({ commit, state }, types) {
      var res = await common().MENU_PERMISSION()
      if (res.code === 0) {
        state.permission = res.data || []
        commit('setMenuPermission', res.data)
        return state.permission
      }
    },

    async getDepartment({ commit, state }, types) {
      var res = await terminal().DEPARTMENT_LIST()
      if (res.code === 0) {
        state.department = res.data || []
        commit('setDepartment', res.data)
        return state.department
      }
    }
  },
  getters: {
    getHeadOptions(state) {
      return state.headOptions || {}
    },
    getMenuPermission: (state) => (type) => {
      const index = state.permission.findIndex(a => a === type)
      return index !== -1
    },
    getDepartment: (state) => (type) => {
      return state.department || []
    }
  }
}
